﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BabyToys.Models;
using System.Data;
namespace BabyToys.Controllers
{
    public class AdminDanhMucController : Controller
    {
        //
        // GET: /AdminDanhMuc/
        DatabaseContext db = new DatabaseContext();
        #region Xai chung
        public List<DanhMucDropdownListItem> DropdownListDanhMuc()
        {
            List<DanhMucDropdownListItem> danhmucs = new List<DanhMucDropdownListItem>();
            foreach (var danhmuc in db.DanhMucs.Where(d => d.DanhMucCha.IdDanhMuc == null))
            {
                danhmucs.Add(new DanhMucDropdownListItem { Value = danhmuc.IdDanhMuc, Text = danhmuc.TenDanhMuc });
                string prefix = "";
                LayDanhMuc(danhmucs, danhmuc.IdDanhMuc, prefix);
            }
            return danhmucs;
        }
        public void LayDanhMuc(List<DanhMucDropdownListItem> danhmucs, int id, string prefix)
        {
            var dsdanhmuc = db.DanhMucs.Where(d => d.DanhMucCha.IdDanhMuc == id);
            string tmp_prefix = prefix;
            if (dsdanhmuc != null)
            {
                foreach (var danhmuc in dsdanhmuc)
                {
                    string text = tmp_prefix + "----" + danhmuc.TenDanhMuc;
                    danhmucs.Add(new DanhMucDropdownListItem { Value = danhmuc.IdDanhMuc, Text = text });
                    prefix += "----";
                    LayDanhMuc(danhmucs, danhmuc.IdDanhMuc, prefix);
                }
            }
        }
        #endregion

        public ActionResult Index(int? id)
        {
            if (Models.User.CurrentUser.Quyen.HasPermission("xem_danh_muc"))
            {
                //List<DanhMucDropdownListItem> danhmucDropdowList = DropdownListDanhMuc(id);
                //ViewBag.DanhMuc = DropdownListDanhMuc();
                var danhmucs = db.DanhMucs.ToList().Where(d => d.IdDanhMuc != 1);
                return View(danhmucs);
            }
            return RedirectToAction("Error", "Admin");
        }
        
        public ActionResult CapNhatHienThi(int id, bool hienthi)
        {
            var danhmuc = db.DanhMucs.SingleOrDefault(s => s.IdDanhMuc == id);
            if (danhmuc != null)
            {
                danhmuc.HienThi = hienthi;
                db.Entry(danhmuc).State = EntityState.Modified;
                db.SaveChanges();
            }
            return View();
        }

        public ActionResult ThemDanhMuc()
        {
            ViewBag.IdDanhMucCha = new SelectList(DropdownListDanhMuc(),"Value","Text");
            return View();
        }

        [HttpPost]
        public ActionResult ThemDanhMuc(DanhMuc danhmuc,FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                int idDanhMucCha = int.Parse(collection["IdDanhMucCha"]);
                var danhmuccha = db.DanhMucs.SingleOrDefault(d => d.IdDanhMuc == idDanhMucCha);
                danhmuc.DanhMucCha = danhmuccha;
                db.DanhMucs.Add(danhmuc);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IdDanhMucCha = new SelectList(DropdownListDanhMuc(), "Value", "Text");
            return View(danhmuc);
        }

        public ActionResult SuaDanhMuc(int id)
        {
            var danhmuc = db.DanhMucs.SingleOrDefault(d => d.IdDanhMuc == id);
            ViewBag.IdDanhMucCha = new SelectList(DropdownListDanhMuc(), "Value", "Text",danhmuc.DanhMucCha.IdDanhMuc);
            return View(danhmuc);
        }

        [HttpPost]
        public ActionResult SuaDanhMuc(DanhMuc danhmuc, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                int idDanhMucCha = int.Parse(collection["IdDanhMucCha"]);
                var danhmuccha = db.DanhMucs.SingleOrDefault(d => d.IdDanhMuc == idDanhMucCha);
                danhmuc.DanhMucCha = danhmuccha;
                db.Entry(danhmuc).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IdDanhMucCha = new SelectList(DropdownListDanhMuc(), "Value", "Text",danhmuc.DanhMucCha.IdDanhMuc);
            return View(danhmuc);
        }
        #region Xoa (bo)
        //public ActionResult Xoa(int id)
        //{
        //    XoaDanhMuc(id);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        //[HttpPost]
        //public ActionResult XoaNhieuDanhMuc(int[] deleteInputs)
        //{
        //    if (deleteInputs != null)
        //    {
        //        foreach (var item in deleteInputs)
        //        {
        //            var danhmuc = db.DanhMucs.SingleOrDefault(d => d.IdDanhMuc == item);
        //            XoaDanhMuc(danhmuc.IdDanhMuc);
        //            db.SaveChanges();
        //            TempData["ThongBao"] = "<script>$( document ).ready(function(){ alert('Xóa thành công!') })</script>";
        //            return RedirectToAction("Index");
        //        }
        //    }
        //    TempData["ThongBao"] = "<script>$( document ).ready(function(){ alert('Bạn chưa chọn danh muc!') })</script>";
        //    return RedirectToAction("Index");
        //}
        //public void XoaDanhMuc(int id)
        //{
        //    var danhmuc = db.DanhMucs.SingleOrDefault(m => m.IdDanhMuc == id);
        //    var danhmuccons = db.DanhMucs.Where(p => p.DanhMucCha.IdDanhMuc == id);
        //    if (danhmuccons.Count() > 0)
        //    {
        //        foreach (var dm in danhmuccons)
        //        {
        //            XoaDanhMuc(dm.IdDanhMuc);
        //        }
        //        //db.SaveChanges();
        //    }
        //    db.DanhMucs.Remove(danhmuc);
        //}
        #endregion

        [NonAction]
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            User su = (User)Session["admin"];
            if (su == null)
            {
                filterContext.Result = new RedirectResult("~/AdminLogin");
            }
            else
            {
                Models.User.CurrentUser = su;
            }
            base.OnActionExecuting(filterContext);
        }
    }
}
